package project.controllers.workers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;

import project.controllers.sort.SortOrderByDeadlineDate;
import project.model.IGarage;
import project.model.IOrder;
import project.model.OrderStatus;

public class WorkerWithOrder {
	
private static final String ORDER_NUMBER="Order #";
private static final String PERFORMED_BY_MASTER = " performed by master ";
private static final String NEXT_FREE_DATE= "Next free date: ";
private static final String ORD_DATE_CHANGE = "Order's deadline change on ";

	static void getMastersByOrder(IOrder order) {
		System.out.println(ORDER_NUMBER+ order.getNumberOrder()
				+ PERFORMED_BY_MASTER + order.getMaster().getNameMaster());
	}

	static void getListMadingOrder(ArrayList<IOrder> orderList,
			Comparator sortby) { 
		ArrayList<IOrder> ordList = new ArrayList<IOrder>();
		Date date = new Date();
		for (IOrder ord : orderList) {
			if (ord.getBeginingDate().before(date)
					&& ord.getDeadlineDate().after(date)
					&& ord.getOrderStatus() == null) {
				ordList.add(ord);
			}
		}
		Collections.sort(ordList, sortby);
		for (IOrder order : ordList) {
			System.out.println(ORDER_NUMBER + order.getNumberOrder());
		}

	}

	static void SortOrder(ArrayList<IOrder> orderList, Comparator sortBy) {
		Collections.sort(orderList, sortBy);
		for (IOrder order : orderList) {
			System.out.println(ORDER_NUMBER + order.getNumberOrder());
		}
	}

	static void getNextFreeDate(ArrayList<IOrder> orderList) {

		Collections.sort(orderList, new SortOrderByDeadlineDate());
		GregorianCalendar temp = orderList.get(0).getDeadlineDate();
		temp.add(GregorianCalendar.DAY_OF_YEAR, 1);
		System.out.println(NEXT_FREE_DATE
				+ temp.getTime().toLocaleString().substring(0, 10));
	}

	static void getOrderByPeriod(ArrayList<IOrder> orderList,
			Comparator sortby, OrderStatus ordSt, GregorianCalendar begin,
			GregorianCalendar end) { 
		ArrayList<IOrder> ordList = new ArrayList<IOrder>();

		for (IOrder ord : orderList) {
			if ((ord.getBeginingDate().after(begin)
					&& ord.getBeginingDate().before(end) || ord.getDateStat()
					.after(begin) && ord.getDateStat().before(end))
					&& ord.getOrderStatus() == ordSt) {
				ordList.add(ord);
			}
		}
		Collections.sort(ordList, sortby);
		for (IOrder order : ordList) {
			System.out.println(ORDER_NUMBER + order.getNumberOrder());
		}
	}

	static void addOrder(ArrayList<IOrder> orderList, IOrder order) {
		orderList.add(order);
	}

	static void markOrder(ArrayList<IOrder> orderList,
			ArrayList<IGarage> garageList, OrderStatus ordSt, IOrder order,
			GregorianCalendar nowDate) {
		for (IOrder o : orderList) {
			if (o.equals(order)) {
				o.setOrderStatus(ordSt);
				o.setDateStat(nowDate);
				o.getMaster().markMasterFree();
				for (IGarage garage : garageList) {
					if (garage.getOrder().equals(order)) {
						garage.markGarageEmpty();
					}
				}
			}
		}
	}
	
	static void ShirtTimeOrder(ArrayList<IOrder> orderList,GregorianCalendar newDate, IOrder order) {
		for (IOrder ord : orderList) {
			if (ord.equals(order)) {
				ord.shiftTime(newDate);
				System.out.println(ORD_DATE_CHANGE
						+ ord.getDeadlineDate().getTime().toLocaleString().substring(0,10));
			}
		}
	}

}
